﻿using Microsoft.AspNetCore.Mvc;
using MyERP.Services;

namespace MyERP.Controllers
{
    public class CaptchaController : Controller
    {
        private readonly CaptchaService _captchaService;

        public CaptchaController(CaptchaService captchaService)
        {
            _captchaService = captchaService;
        }

        // 返回验证码图片
        public IActionResult GetCaptchaImage()
        {
            // 生成验证码文本
            var captchaCode = _captchaService.GenerateCaptchaCode();

            // 将验证码文本存储到 Session 中
            HttpContext.Session.SetString("CaptchaCode", captchaCode);

            // 生成验证码图片
            var imageBytes = _captchaService.GenerateCaptchaImage(captchaCode);

            // 返回图片
            return File(imageBytes, "image/png");
        }
    }
}
